package com.zhou.yuefan.activity;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.zhou.yuefan.R;
import com.zhou.yuefan.R.drawable;
import com.zhou.yuefan.application.MyApplication;
import com.zhou.yuefan.db.DBManager;
import com.zhou.yuefan.model.Food;
import com.zhou.yuefan.model.Shop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class FoodsActivity extends Activity {
	private ImageView img_foodBanner;
	private ListView lv_foods;
	private MyApplication myApplication;
	DBManager dbManager;
	SQLiteDatabase db;
	// Context context;
	// List<Food> foodsList = new ArrayList<Food>();
	View ll_foodItem;
	TextView tv_foodName;
	TextView tv_foodPrice;
	LinearLayout ll_foodBanner;
	Button btn_call;
	Button btn_cancel;
	Bitmap bitmap;
	List<Food> foodsList = new ArrayList<Food>();
	int shop_id_from;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		
		setContentView(R.layout.foods);
		//解决PNG渐变失真问题
				getWindow().setFormat(PixelFormat.RGBA_8888);
		Intent intentFromShop = this.getIntent();
		shop_id_from = intentFromShop.getIntExtra("shop_id", 1);
		img_foodBanner = (ImageView) findViewById(R.id.img_foodsbanner);
		ll_foodBanner = (LinearLayout) findViewById(R.id.ll_foodsbanner);
		AssetManager assetManager = this.getResources().getAssets();
		InputStream is;
		try {
			is = assetManager.open("shop_banner.png");
			bitmap = BitmapFactory.decodeStream(is);

			// Drawable drawable = Drawable.createFromStream(is,
			// "shop_nnmg.PNG");
			img_foodBanner.setImageBitmap(bitmap);
			// bitmap.recycle();
			is.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		lv_foods = (ListView) findViewById(R.id.lv_foods);
		myApplication = (MyApplication) getApplication();

		dbManager = myApplication.getDBInstance();
		// db = dbManager.openDatabase();
		foodsList = dbManager.getFoodList(shop_id_from);

		FoodsAdapter adapter = new FoodsAdapter(this, foodsList);
		lv_foods.setAdapter(adapter);
		btn_call = (Button) findViewById(R.id.btn_call);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		btn_call.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String mobile = foodsList.get(1).getShop().getPhone1();

				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ mobile));

				FoodsActivity.this.startActivity(intent);
			}
		});
	}

	class FoodsAdapter extends BaseAdapter {
		Context context;
		LayoutInflater inflater;
		List<Food> data;

		public FoodsAdapter(Context context, List<Food> data) {

			super();
			this.context = context;
			this.data = data;
			this.inflater = LayoutInflater.from(context);

		}

		@Override
		public int getCount() {
			return data.size();

		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return data.get(position).getId();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.lv_item, null);
			}
			TextView tv_foodName = (TextView) convertView
					.findViewById(R.id.tv_foodText);
			TextView tv_foodPrice = (TextView) convertView
					.findViewById(R.id.tv_foodPrice);
			tv_foodName.setText(data.get(position).getFood_name());
			tv_foodPrice.setText("￥ "+data.get(position).getPrice() + "");

			return convertView;
		}

	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (bitmap != null) {
			bitmap.recycle();
		}
	}
	
}
